package com.jelly.web.modules.monitor.log.service.impl;

import com.jelly.common.util.StringUtils;
import com.jelly.web.modules.monitor.log.dao.LoginLogMapper;
import com.jelly.web.modules.monitor.log.domain.LoginLog;
import com.jelly.web.modules.monitor.log.domain.LoginLogExample;
import com.jelly.web.modules.monitor.log.service.ILoginLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @创建人 ql
 * @创建时间 2018-08-17 14:42
 * @功能描述: 登录日志记录接口实现类
 **/
@Service
public class LoginLogServiceImpl implements ILoginLogService {

    @Autowired
    private LoginLogMapper loginLogMapper;

    /**
     * 新增登录日志记录
     * @param loginLog
     * @return
     */
    @Override
    public boolean insterLoginLog(LoginLog loginLog) {
        loginLog.setLoginId(UUID.randomUUID().toString().replaceAll("-", ""));
        loginLog.setAddDate(new Date());
        return loginLogMapper.insertSelective(loginLog) > 0;
    }

    /**
     * 获取登录日志列表
     * @param loginLog
     * @return
     */
    @Override
    public List<LoginLog> getLoginLog(LoginLog loginLog) {
        LoginLogExample example = new LoginLogExample();
        LoginLogExample.Criteria criteria = example.createCriteria();

        if(StringUtils.isNotBlank(loginLog.getLoginAccount())){
            criteria.andLoginAccountEqualTo(loginLog.getLoginAccount());
        }

        if(StringUtils.isNotBlank(loginLog.getIsState())){
            criteria.andIsStateEqualTo(loginLog.getIsState());
        }
        if(StringUtils.isNotBlank(loginLog.getStartTime()) && StringUtils.isNotBlank(loginLog.getEndTime())){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try{
                criteria.andAddDateGreaterThanOrEqualTo(sdf.parse(loginLog.getStartTime()));
                criteria.andAddDateLessThanOrEqualTo(sdf.parse(loginLog.getEndTime()));
            }catch (Exception e){

            }
        }

        return loginLogMapper.selectByExample(example);
    }
}
